package com.example.ui_view;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.ui_view.model.user;

import java.util.Random;

public class ProgressBarActivity extends AppCompatActivity {
    private ProgressBar progressBar;
    private Button btnStart;
    TextView tvtext;
    boolean stops = true;
    Random random = new Random();
    int value=0;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_bar);
        btnStart=findViewById(R.id.btnStart);
        tvtext = findViewById(R.id.textView6);
        progressBar = findViewById(R.id.progressBar2);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what==0x110){
                    super.handleMessage(msg);
                    progressBar.setProgress(msg.arg1);
                    user u = (user) msg.obj;
                    tvtext.setText("value"+u.getId()+"  "+msg.arg1);
                }

            }
        };

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog = new AlertDialog.Builder(ProgressBarActivity.this).create();
                dialog.setTitle("友情提示！");
                dialog.setMessage("进度条正在加载......");
                dialog.setIcon(R.mipmap.ic_launcher);
                dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {

                                while (stops){
                                    value = value+random.nextInt(10);
                                    if(value>=100){
                                        value = 100;
                                        stops=false;
                                    }

//                            progressBar.setProgress(value);
//                            tvtext.setText("value"+value);
                                    if(handler!=null){

                                        Message message = new Message();
                                        user u = new user("1830416","张三");
                                        message.arg1 = value;
                                        message.obj = u;
                                        message.what = 0x110;

                                        handler.sendMessage(message);
                                    }

                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }

                                }

                            }
                        }).start();
                    }
                });
                dialog.show();

            }
        });
    }
}
